package com.zpark.controller.exception;

import org.springframework.boot.autoconfigure.web.servlet.error.AbstractErrorController;
import org.springframework.boot.web.error.ErrorAttributeOptions;
import org.springframework.boot.web.servlet.error.ErrorAttributes;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * @PackageName: com.zpark.controller.exception
 * @ClassName: CustomExceptionHandlerController
 * @Description:
 * @author: RZS
 * @date: 2022/9/22  16:35
 */
@Controller
//@RestController
public class CustomExceptionHandlerController extends AbstractErrorController {
    // 定义错误映射路径
    private final static String ERROR_PATH = "/error";

    // 添加构造器
    public CustomExceptionHandlerController(ErrorAttributes errorAttributes) {
        // 调用父类构造器创建父类对象
        super(errorAttributes);
    }

    @Override
    public String getErrorPath() {
        return ERROR_PATH;
    }

//    @RequestMapping(ERROR_PATH)
//    public Map<String, Object> customHandler(HttpServletRequest request){
//
//        // 获取错误信息map对象
//        /**
//         * getErrorAttributes()方法的返回Map对象中的内容如下：
//         *      {
//         *          "timestamp":"2022-09-21T03:47:38.030+00:00",
//         *          "status":404,
//         *          "error":"Not Found",
//         *          "message":"No message available",
//         *          "path":"/exception/12"
//         *       }
//         */
//        Map<String, Object> map = getErrorAttributes(request, ErrorAttributeOptions.of(ErrorAttributeOptions.Include.MESSAGE));
//
//
//        // 自定义返回的数据类型
//        Map<String, Object> result = new HashMap<>();
//        // 封装错误信息数据
//        result.put("code", map.get("status"));
//        result.put("msg", map.get("error"));
//
//        return result;
//    }

    @RequestMapping(ERROR_PATH)
    public ModelAndView customHandler(HttpServletRequest request){

        // 创建视图解析对象
        ModelAndView mv = new ModelAndView();
        // 设置视图解析名字
        mv.setViewName("404");

        // 获取错误信息map对象
        /**
         * getErrorAttributes()方法的返回Map对象中的内容如下：
         *      {
         *          "timestamp":"2022-09-21T03:47:38.030+00:00",
         *          "status":404,
         *          "error":"Not Found",
         *          "message":"No message available",
         *          "path":"/exception/12"
         *       }
         */
        Map<String, Object> map = getErrorAttributes(request, ErrorAttributeOptions.of(ErrorAttributeOptions.Include.MESSAGE));


        // 自定义返回的数据类型
        Map<String, Object> result = new HashMap<>();
        // 封装错误信息数据
        result.put("code", map.get("status"));
        result.put("msg", map.get("error"));

        // 绑定数据
        mv.addObject("res", result);

        return mv;
    }

}
